package cn.bon.user.service.impl;

import cn.bon.common.bean.JsonResult;
import cn.bon.common.enums.bonuser.RoleStateEnum;
import cn.bon.common.tool.R;
import cn.bon.user.mapper.RoleMapper;
import cn.bon.user.pojo.po.RolePo;
import cn.bon.user.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: dronff
 * @description:
 * @CreateTime: 2023/12/11 11:13
 **/

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RolePo> implements RoleService {
    @Override
    public JsonResult<List<RolePo>> pageRoles(String roleName, RoleStateEnum type) {
        if (type == null) {
            return R.success(
                    list(
                            new QueryWrapper<RolePo>()
                                    .like("role_alias","%" + (roleName == null ? "" : roleName) + "%")
                                    .orderByAsc("role_order")
                    )
            );
        }

        return R.success(
                list(
                        new QueryWrapper<RolePo>()
                                .like("role_alias","'%" + (roleName == null ? "" : roleName) + "%'")
                                .eq("role_state",type.getCode())
                                .orderByAsc("role_order")
                )
        );
    }

    @Override
    public JsonResult deleteRoleById(int roleId) {
        if (removeById(
                roleId
        )) {
            return R.success();
        }
        return R.fail();
    }
}
